<template>
    <div class="login-container"
        :style="{ backgroundImage: `url(https://gd-hbimg.huaban.com/d6d45420a5ec7ddd340125915c3cbe688c7ac3ef45faf8-gkTYsT_fw658)` }">
        <div class="login-content">
            <el-card shadow="never" class="login-card">
                <h2 style="padding: 0 0 30px 0;">欢迎登录</h2>
                <el-form :model="form" status-icon ref="loginForm" label-width="80px">
                    <el-form-item label="用户名" prop="username" :rules="[
            {
                required: true,
                message: '请输入用户名',
                trigger: 'blur',
            }
        ]">
                        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password" :rules="[
            {
                required: true,
                message: '请输入密码',
                trigger: 'blur',
            }
        ]">
                        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="handleLogin(loginForm)">登录</el-button>
                        <el-button>重置</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { inject, ref } from 'vue';
const http: any = inject('http');
import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';

import { useRouter } from 'vue-router';
const router = useRouter();

const loginForm = ref<FormInstance>()

const form = ref({
    username: '',
    password: ''
});

const handleLogin = (loginForm: FormInstance | undefined) => {
    if (!loginForm) return
    loginForm.validate((valid) => {
        if (valid) {
            const { username, password } = form.value;
            http.post('/api/login', {
                username, password
            }).then((res: any) => {
                if (res.code === 1) {
                    window.localStorage.setItem('token', res.data.token);
                    window.localStorage.setItem('username', res.data.username);
                    ElMessage({
                        message: '登陆成功',
                        type: 'success',
                    })
                    setTimeout(() => {
                        router.replace({
                            path: '/',
                        })
                    }, 2000)
                }
            })
        } else {
            console.log('error submit!')
            return false
        }
    })
};
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-size: cover;
    background-position: center;
    transition: background 0.5s ease-in-out;
}

.login-content {
    width: 400px;
}

.login-card {
    box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.1);
    padding: 20px;
    border-radius: 10px;
    background-color: rgba(255, 255, 255, 0.9);
}

.login-form-item {
    margin-bottom: 20px;
}

.login-btn-container {
    margin-top: 20px;
}

::v-deep .el-form-item__content {
    display: flex;
    justify-content: space-between;
}
</style>